package com.igame.Controller;

import com.igame.Service.UserService;
import com.igame.common.Result;
import com.igame.dto.*;
import com.igame.entity.PasswordSecurity;
import com.igame.entity.User;
import com.igame.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Set;

@CrossOrigin //忽视跨域问题
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping("/register")
    public Result Register(@RequestBody UserDto userDto){
        return userService.Register(userDto);
    }

    @PostMapping("/login")
    public Result<User> test(@RequestBody UserDto userDto){
        return userService.login(userDto);
    }
    @GetMapping("/toLogin")
    public String toLogin(){
        return "Login";
    }

    @GetMapping("/goOrther")
    public String goOrther(){
        return "Orther";
    }

    @GetMapping("/Avatar")
    public String getAvatar(){
        return "Avatar";
    }

    @PostMapping("/getAvatar")
    public Result<String> getAvatarByUserName(@RequestBody Map<String, String> requestBody){
        String username = requestBody.get("username");
        return userService.getAvatarByUserName(username);
    }

    @PostMapping("/getUpUser")
    public Result<UserVo> getUpUser(@RequestBody String username){
        String cleanedUsername = username.replace("\"", "");
        return userService.getUpUser(cleanedUsername);
    }
    @PostMapping("/follow")
    public Result follow(@RequestBody FollowDto followDto){
        return  userService.addFollow(followDto);
    }

    @PostMapping("/getUserNameById")
    public Result<Set<String>> getUserNameById(@RequestBody List<Integer> response){
      return userService.getUserNameById(response);
    }
    @PostMapping("/getUser")
    public Result<UserVo> getUser(@RequestBody Integer id){
        return userService.getUserById(id);
    }

    @GetMapping("/areas")
    public AreaResponse getArea(){
       return userService.getAllArea();
    }

    @PostMapping("/updateUser")
    public Result updateUser(
            @RequestParam("id") Integer id,
            @RequestParam("username") String username,
            @RequestParam("provinceName") String provinceName,
            @RequestParam("cityName") String cityName,
            @RequestParam("districtName") String districtName,
            @RequestParam("address") String address,
            @RequestPart(value = "file", required = false) MultipartFile file) {
        return  userService.update(id,username,provinceName,cityName,districtName,address,file);
    }

    @PostMapping("/verifyPassword")
    public Result verifyPassword(@RequestBody UserDto userDao){
        return userService.verifyPassword(userDao);
    }

    @PostMapping("/setSecurityQuestions")
    public Result setSecurityQuestions(@RequestBody SecurityQuestionsRequestDTO request) {
        return userService.addPasswordSecurity(request);
    }

    @PostMapping("/getSecurityQuestions")
    public Result<List<PasswordSecurity>> getSecurityQuestions(@RequestBody Integer loginId) {
        return userService.userHavePds(loginId);
    }

    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody ChangePasswordDto changePasswordDto){
        return userService.changePassword(changePasswordDto);
    }

    @PostMapping("/sendEmailCode")
    public Result<String> sendEmailCode(@RequestBody String email){
        return userService.sendCode(email);
    }

    @PostMapping("/forgotPassword")
    public Result forgotPassword(@RequestBody ChangePasswordDto changePasswordDto){
       return userService.forgotPassword(changePasswordDto);
    }

    @PostMapping("/getPasswordSecurity")
    public Result<List<PasswordSecurity>> getPasswordSecurity(@RequestBody String username) {
        return userService.getPasswordSecurity(username);
    }


}
